package com.liunian.algorithmstudy.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class LevelOrder429 {

	static class Node {
		public int val;
		public List<Node> children;

		public Node() {
		}

		public Node(int _val) {
			val = _val;
		}

		public Node(int _val, List<Node> _children) {
			val = _val;
			children = _children;
		}
	}

	public List<List<Integer>> levelOrder(Node root) {
		if (root == null) {
			return Collections.emptyList();
		}
		Deque<Node> deque = new LinkedList<>();
		List<List<Integer>> res = new ArrayList<>();
		deque.push(root);
		while (!deque.isEmpty()) {
			int size = deque.size();
			List<Integer> temp = new ArrayList<>();
			while (size-- > 0) {
				Node node = deque.pollFirst();
				temp.add(node.val);
				for (Node child : node.children) {
					deque.addLast(child);
				}
			}
			res.add(temp);
		}
		return res;
	}

}
